import {Component, OnInit,OnDestroy} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 *
 * 空气质量
 * zengxianxiang
 */
export class AirQuality{
  constructor(
    private  userID:string,//用户id
    private  focusPoint:string, //关注点 0：重点  1：非重点
    private  pollutionIDs:any, //污染源企业ID
    private  areaCode:string//行政区域
  ){}
}
export class History{
  constructor(
    private  pointID:string,//企业id
    private  dataType:string, //数据类型
    private  startTime:string,//开始时间
    private  endTime:string,//结束时间
    private  pageNum:string//分页数
  ){}
}
export class MonitorInfo{
  constructor(
    private  userID:string,
    private  pollutionType:string,
  ){}
}
export class UserID{
  constructor(
    private  userID:string,
  ){}
}
@Component({
  selector: 'app-air-quality',
  templateUrl: './air-quality.component.html',
  styleUrls: ['./air-quality.component.css']
})
export class AirQualityComponent implements OnInit,OnDestroy {
  public dimsUrl:string = this.httpURLService.getUrl("DIMS");

  public jsq:number;
  private timer;
  public arr: Array<any>;  //input展示内容
  public arrOne: Array<any>;  //存储pointID
  public arrTwo: Array<any>;  //存储pointID
  public arrs:Array<any>;   //储存input展示内容
  //多选数组
  public itemList: Array<any>;
  public selected: Array<any>;
  public allList: Array<any>;
  public idListAll: Array<any>;

  public userID:string;
  public pollutionInfoNum:string;
  private heads: Array<any>;
  private Properties: Array<any>;
  private DataList: Array<any>;
  private businessInfos: Array<any>;
  private List: Array<any>;
  public pointID:string;
  public AirQualityQuery:AirQuality = new AirQuality("","","","");
  public AirQualityMonitorInfo:MonitorInfo = new MonitorInfo("","");
  public UserID:UserID = new UserID("");
  constructor(private http: HttpClient,private httpURLService: HttpURLService) {
    //多选框所需数组
    this.itemList = [1, 2, 3, 4];
    this.allList = [1, 2, 3, 4];
    this.idListAll = [1, 2, 3, 4];
    this.selected = [];
    this.arr = [];
    this.arrs = [];
    this.arrOne = [];
    this.userID = sessionStorage["userID"];

    this.jsq = 30;
    this.timer = setInterval(() => {
      if(this.jsq<1){
        this.jsq=31;
      }
      if(this.jsq==30){
        //首次加载实时
        this.AirQualityQuery  =  new AirQuality(this.userID,"","","");
        this.http.post(this.dimsUrl+'/environment/queryAirQltyRealTimeMon',this.AirQualityQuery).subscribe(data => {
          this.heads = data['bussRealtimeDatas'].head;
          this.Properties = data['bussRealtimeDatas'].properties;
          this.DataList = data['bussRealtimeDatas'].dataList;
          this.pollutionInfoNum = data['pollutionInfoNum'];
        });
      }
      this.jsq -= 1;
    }, 1000);

    //首次加载污染企业
    this.AirQualityMonitorInfo = new MonitorInfo(this.userID,"1");
    this.http.post(this.dimsUrl+'/business/queryBusinessInfo',this.AirQualityMonitorInfo).subscribe(data => {
      this.businessInfos = data["businessInfos"];
    });
    //首次加载行政区域
    this.UserID = new UserID(this.userID);
    this.http.post(this.dimsUrl+'/adminArea/queryAdminArea',this.UserID).subscribe(data => {
      this.List = data["list"];
    });
  }
  //查询执行
  listMessage(info:any){
    clearInterval(this.timer);
    //获取input和select内容
    let focusPoint = info.focusPoint== undefined ? '' : info.focusPoint;
    let pollutionIDs = this.arrTwo== [] ? '' : this.arrTwo;
    let areaCode = info.areaCode== undefined ? '' : info.areaCode;
    //按内容查找
    this.userID = sessionStorage["userID"];
    this.AirQualityQuery  =  new AirQuality(this.userID,focusPoint,pollutionIDs,areaCode);
    this.jsq = 30;
    this.timer = setInterval(() => {
      if(this.jsq<1){
        this.jsq=31;
      }
      if(this.jsq==30){
        this.http.post(this.dimsUrl+'/environment/queryAirQltyRealTimeMon',this.AirQualityQuery).subscribe(data => {
          this.heads = data['bussRealtimeDatas'].head;
          this.Properties = data['bussRealtimeDatas'].properties;
          this.DataList = data['bussRealtimeDatas'].dataList;
          this.pollutionInfoNum = data['pollutionInfoNum'];
        });
      }
      this.jsq -= 1;
    }, 1000);
  }
  //跳转历史并将数据存储本地
  aaa(e){
    sessionStorage["pointID"]=this.DataList[e].pointID;
    sessionStorage["url"] = [];
    sessionStorage["url"]="/environment/queryAirQltyHistoricalMon";
  }
  //判断是否为数组
  isArray(arr){
    return Array.isArray(arr);
  }
  ngOnDestroy() {
    clearInterval(this.timer);
  }
  ngOnInit(){
  }
  //污染企业显示隐藏
  shows(){
    $(".Polluters").stop().slideDown();
  }
  hides(){
    $(".Polluters").stop().slideUp();
  }
  //   点击时执行
  clickItem(e, item,text,id) {
    const checkbox = e.target;
    const action = (checkbox.checked ? 'add' : 'remove');
    this.updateSelected(action, item);
    if(action == "add"){
      this.arrs.push(text);
      this.arrOne.push(id);
    }
    if(action == "remove"){
      for(let i=0;i<this.arrs.length;i++){
        if(text == this.arrs[i]){
          this.arrs.splice(i,1);
          this.arrOne.splice(i,1);
        }
      }
    }
    this.arr = this.arrs;
    this.arrTwo = this.arrOne;
  }
  // 用来判断input 的checked
  isCheck(item) {
    return this.selected.findIndex(value => value == item) >= 0;
  }
  //  执行增加、删除
  private updateSelected(action, item) {
    if (action == 'add' && this.selected.findIndex(value => value == item) == -1){
      this.selected.push(item);
    }
    if (action == 'remove' && this.selected.findIndex(value => value == item) != -1){
      this.selected.splice(this.selected.findIndex(value => value == item), 1);
    }
  }
  // 全选点击事件
  selectAll(e) {
    const checkbox = e.target;
    const action = (checkbox.checked ? 'add' : 'remove');
    this.allList.forEach((elt, i, array) => {
      const entity = elt;
      this.updateSelected(action, entity);
    });
    if(action == "add"){
      this.businessInfos.forEach((elt, i, array) => {
        this.arrs.push(array[i]["businessName"]);
        this.arrOne.push(array[i]["pointID"]);
      });
    }
    if(action == "remove"){
      this.arrs = [];
      this.arrOne = [];
    }
    this.arr = this.arrs;
    this.arrTwo = this.arrOne;
  }

  // 判断是否全选
  isSelectedAll() {
    return this.isContained(this.selected, this.idListAll);
  }
  // 判断b数组是否包含在a数组中
  private isContained(a, b) {
    if (!(a instanceof Array) || !(b instanceof Array)) return false;
    if (a.length < b.length) return false;
    const aStr = a.toString();
    for (let i = 0, len = b.length; i < len; i++) {
      if (aStr.indexOf(b[i]) == -1) {
        return false;
      }
    }
    return true;
  }
}




